﻿
// colorDlg.cpp: 实现文件
//
#include "pch.h"
#include "framework.h"
//#include "t2Dll1.h"
#include "color.h"

#include "colorDlg.h"
#include "afxdialogex.h"
#include <iostream>
#include <string>
#include "CocrMatrix.h"
//#pragma comment(lib,"T2Dll1.lib")//静态加载动态链接库

using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CcolorDlg 对话框



CcolorDlg::CcolorDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_COLOR_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CcolorDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_IMG, img);
	DDX_Control(pDX, IDC_EDIT1, edit1);
	DDX_Control(pDX, IDC_COMBO1, cb1);
	DDX_Control(pDX, IDC_EDIT4, edit4);
	DDX_Control(pDX, IDC_EDIT5, edit5);
}

BEGIN_MESSAGE_MAP(CcolorDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//ON_WM_MOUSEMOVE()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDOK1, &CcolorDlg::OnBnClickedOk1)
	ON_STN_CLICKED(IDC_IMG, &CcolorDlg::OnStnClickedImg)
	ON_EN_CHANGE(IDC_EDIT1, &CcolorDlg::OnEnChangeEdit1)
	ON_CBN_SELCHANGE(IDC_COMBO1, &CcolorDlg::OnCbnSelchangeCombo1)
	ON_BN_CLICKED(IDTEST1, &CcolorDlg::OnBnClickedTest1)
	ON_BN_CLICKED(IDtest2, &CcolorDlg::OnBnClickedTest2)
	//ON_COMMAND(ID_F1, &CcolorDlg::OnF1)
	//ON_COMMAND(ID_F2, &CcolorDlg::OnF2)
	ON_MESSAGE(WM_HOTKEY, &CcolorDlg::OnHotKey)
END_MESSAGE_MAP()



void CcolorDlg::OnClose() {
	UnregisterHotKey(GetSafeHwnd(), 1001);
	UnregisterHotKey(GetSafeHwnd(), 1002);
	CDialogEx::OnClose();
}
// CcolorDlg 消息处理程序

BOOL CcolorDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码
	img=(CStatic*)GetDlgItem(IDC_IMG);
	AfxGetApp()->m_pMainWnd->SetWindowText(_T("取色器"));
	img->MoveWindow(30, 30, 100, 100, true);
	GetDlgItem(IDC_EDIT2)->SetWindowText(_T("0"));
	GetDlgItem(IDC_EDIT3)->SetWindowText(_T("0"));
	GetDlgItem(IDC_EDIT3)->SetWindowText(_T("0"));
	edit5.SetWindowText(_T("62|585|5|8|0xffffff"));


	m_hAccel = ::LoadAccelerators(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_ACCELERATOR1));
	SetTimer(1, 100, NULL);



	if (!RegisterHotKey(m_hWnd, 1001, MOD_CONTROL,VK_F1))
	{
		AfxMessageBox(_T("Ctrl + f1 快捷键冲突!"));
	}

	if (!RegisterHotKey(m_hWnd, 1002, MOD_CONTROL, VK_F2))
	{
		AfxMessageBox(_T("Ctrl + f2 快捷键冲突!"));
	}
	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CcolorDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CcolorDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();

	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CcolorDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CcolorDlg::OnMouseMove(UINT nFlags, CPoint point)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
	POINT pt;
	CWnd* pWnd = GetDesktopWindow();
	GetCursorPos(&pt); // 获取当前鼠标位置

	//CPoint point_g;
	//GetCursorPos(&point_g);
	CString str;
	//str.Format(L"客户区鼠标位置：x%d y%d 屏幕区鼠标位置：x%d y%d", point.x, point.y, point_g.x, point_g.y);

		//原点坐标
	CString strx;
	CString stry;
	GetDlgItemText(IDC_EDIT2, strx);
	GetDlgItemText(IDC_EDIT3, stry);
	int numx = std::stoi(strx.GetString());
	int numy = std::stoi(stry.GetString());

	str.Format(_T("%d"), pt.x-numx);
	GetDlgItem(IDC_STATIC2)->SetWindowText(str);
	str.Format(_T("%d"), pt.y-numy);
	GetDlgItem(IDC_STATIC4)->SetWindowText(str);

	
	


	//int R;
	//int G;
	//int B;
	//GetCursorPos(&pt);
	//HDC hDC = GetDC(NULL);
	//COLORREF color = GetPixel(hDC, pt.x, pt.y);
	//R = GetRValue(color);
	//G = GetGValue(color);
	//B = GetBValue(color);
	//std::cout << "x : " << pt.x << ", y : " << pt.y << ", R : " << R << ", G : " << G << ", B : " << B << endl;
	//ReleaseDC(NULL, hDC);
	CDialog::OnMouseMove(nFlags, point);
}

void CcolorDlg::OnTimer(UINT_PTR nIDEvent) {
	CString str1;
	CString strx;
	CString stry;
	int numx;
	int numy;
	COLORREF color;
	HDC hdc;
	POINT pt;
	GetCursorPos(&pt); // 获取当前鼠标位置
	hdc = ::GetDC(NULL);
	color = GetPixel(hdc, pt.x, pt.y);
	::ReleaseDC(NULL, hdc);

	int r = color & 0xFF;;
	int g = (color >> 8) & 0xFF;
	int b = (color >> 16) & 0xFF;
	switch (nIDEvent)
	{
	case 1:
		
		//CWnd* pWnd = GetDesktopWindow();
		

		//CPoint point_g;
		//GetCursorPos(&point_g);
		
		//str.Format(L"客户区鼠标位置：x%d y%d 屏幕区鼠标位置：x%d y%d", point.x, point.y, point_g.x, point_g.y);

		 // 获取屏幕坐标的颜色
		
		
		
		str1.Format(_T("%02x%02x%02x"), GetBValue(color), GetGValue(color), GetRValue(color));
		str1.MakeUpper();
		GetDlgItem(IDC_STATIC6)->SetWindowText(str1);

		//原点坐标
		GetDlgItemText(IDC_EDIT2, strx);
		GetDlgItemText(IDC_EDIT3, stry);
		 numx = std::stoi(strx.GetString());
		 numy = std::stoi(stry.GetString());
		

		//设置x坐标
		str1.Format(_T("%d"), pt.x-numx);
		GetDlgItem(IDC_STATIC2)->SetWindowText(str1);
		//设置y坐标
		str1.Format(_T("%d"), pt.y - numy);
		GetDlgItem(IDC_STATIC4)->SetWindowText(str1);

		str1.Format(_T("%d"), r);
		GetDlgItem(IDC_STATIC11)->SetWindowText(str1);

		str1.Format(_T("%d"), g);
		GetDlgItem(IDC_STATIC13)->SetWindowText(str1);

		str1.Format(_T("%d"), b);
		GetDlgItem(IDC_STATIC15)->SetWindowText(str1);

		ScreenCapture(img,pt.x, pt.y);
		//CaptureScreen(pt.x, pt.y);
	default:
		break;
	}
}


void ScreenCapture(CStatic* img,int x,int y)
{
	// TODO: 在此添加控件通知处理程序代码
	// 设置截图控件的大小和位置
	//HWND hwnd = GetDesktopWindow();
	//CRect rect;
	//GetClientRect(hwnd,&rect);
	CWnd* pWnd = AfxGetMainWnd();  // 获取主窗口指针
	HWND hWnd = pWnd->GetSafeHwnd();
	// 创建一个与屏幕大小相同的内存设备场景进行绘图
	int w = 10;
	int h = 10;
	HDC hdcScreen = GetDC(NULL);
	
	HDC mdc;
	mdc = CreateCompatibleDC(hdcScreen);
	HBITMAP hbit;
	HBITMAP hold;
	hbit = CreateCompatibleBitmap(hdcScreen, 200,200);
	if (hbit==NULL) {
		MessageBox(NULL, _T("CreateCompatibleBitmap失败"), _T("标题"), MB_OK);
	}
	bool b1;
	int su;
	// 将屏幕图像绘制到内存设备场景中
	/*CImage nImage;
	int bitOfPix = GetDeviceCaps(hdcScreen, BITSPIXEL);
	nImage.Create(200,200, bitOfPix);
	HDC imgHdc = nImage.GetDC();*/
	hold=(HBITMAP)SelectObject(mdc, hbit);
	//BitBlt(mdc, 0, 0,200,200, hdcScreen, x - 10, y - 10, SRCCOPY);
	//CImage image;
	//image.Attach(hbit);


	

	StretchBlt(mdc,0, 0, 200, 200, hdcScreen, x - 5, y - 5, w,h, SRCCOPY);
	//将位图刷新到hbit
	SelectObject(mdc, hold);
	//image.Save(_T("c:\\text.bmp"));
	// 将内存设备场景的图像显示到控件上
	
	HBITMAP temp=img->SetBitmap(hbit);
	
	//很重要否则长时间会内存溢出
	b1 = DeleteObject(temp);

	su = ReleaseDC(NULL, hdcScreen);
	if (su == 0) {
		MessageBox(NULL, _T("ReleaseDC失败"), _T("标题"), MB_OK);
	}
   su = DeleteDC(mdc);
	if (su == 0) {
		MessageBox(NULL, _T("DeleteDC失败"), _T("标题"), MB_OK);
	}
	b1 = DeleteObject(hbit);
	if (b1 == false) {
		MessageBox(NULL, _T("DeleteObject失败"), _T("标题"), MB_OK);
		return;
	}
	

	
	

	CDC* pDC = img->GetWindowDC();
	CPen penRed;
	
	b1=penRed.CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
	if (b1 == false) {
		MessageBox(NULL, _T("CreatePen失败"), _T("标题"), MB_OK);
		return;
	}
	CPen* pOldPen = pDC->SelectObject(&penRed);
	pDC->MoveTo(100, 100);
	pDC->LineTo(100, 120);
	pDC->LineTo(120, 120);
	pDC->LineTo(120, 100);
	pDC->LineTo(100,100);

	pDC->SelectObject(pOldPen);
	b1 = DeleteObject(penRed);
	if (b1 == false) {
		MessageBox(NULL, _T("DeleteObject失败"), _T("标题"), MB_OK);
		return;
	}
	
	// 清理
	su=img->ReleaseDC(pDC);
	if (su == 0) {
		MessageBox(NULL, _T("ReleaseDC失败"), _T("标题"), MB_OK);
	}
}


void CcolorDlg::OnBnClickedOk1()
{
	// TODO: 在此添加控件通知处理程序代码
	//ScreenCapture(&img, 50, 50);
	//test1();
	CRect r;
	if( hWnd == 0){
		::MessageBox(NULL, _T("请选择窗口"), _T("标题"), MB_OK);
	}
	::ShowWindow(hWnd, SW_RESTORE);
	::GetWindowRect(hWnd, r);
	::SetForegroundWindow(hWnd);
	CString str;

	CString str1;
	GetDlgItemText(IDC_EDIT2, str1);
	CString str2;
	GetDlgItemText(IDC_EDIT3, str2);

	//需要管理员权限
	::MoveWindow(hWnd, _ttoi(str1), _ttoi(str2),
		r.right - r.left, r.bottom - r.top, true);

}

void CcolorDlg::OnStnClickedImg()
{
	// TODO: 在此添加控件通知处理程序代码

}


void CcolorDlg::CaptureScreen1(int x, int y)
{
	CDC* pDC = GetWindowDC();
	CDC dcMemory;
	dcMemory.CreateCompatibleDC(pDC);
	HWND hwnd = ::GetDesktopWindow();
	HDC hdc = ::GetDC(hwnd);



	CRect rect;
	GetClientRect(&rect);
	CBitmap bitmap;
	bitmap.CreateCompatibleBitmap(&dcMemory, 20,20);
	CBitmap *pOld=dcMemory.SelectObject(&bitmap);

	// 截取整个屏幕
	::BitBlt(dcMemory.GetSafeHdc(), 0, 0, 20,20,
		hdc, x - 10, y - 10, SRCCOPY);
	CDC* pDC1=img->GetWindowDC();
	pDC1->StretchBlt(0, 0, 200, 200, pDC, 0, 0, 20,20, SRCCOPY);

	//HBITMAP hBitmap = (HBITMAP)dcMemory.SelectObject(bitmap);
	//bitmap.Detach();
	ReleaseDC(pDC1);
	dcMemory.SelectObject(pOld);
	ReleaseDC(&dcMemory);
	ReleaseDC(pDC);
	::ReleaseDC(hwnd, hdc);
}

void SaveBitmapToFile(HBITMAP hBitmap)
{
	BITMAP bmp = { 0 };
	GetObject(hBitmap, sizeof(bmp), &bmp);
	BITMAPFILEHEADER bmFileHeader = { 0 };
	bmFileHeader.bfType = 0x4d42;
	bmFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	bmFileHeader.bfSize = bmFileHeader.bfOffBits + bmp.bmWidthBytes * bmp.bmHeight;
	BITMAPINFOHEADER bmInfoHeader = { 0 };
	bmInfoHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmInfoHeader.biWidth = bmp.bmWidth;
	bmInfoHeader.biHeight = bmp.bmHeight;
	bmInfoHeader.biPlanes = 1;
	bmInfoHeader.biBitCount = bmp.bmBitsPixel;
	bmInfoHeader.biCompression = BI_RGB;
	bmInfoHeader.biSizeImage = bmp.bmWidthBytes * bmp.bmHeight;
	HANDLE hFile = CreateFile(_T("c:\\text.bmp"), GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hFile == INVALID_HANDLE_VALUE) return;
	DWORD dwWritten = 0;
	WriteFile(hFile, &bmFileHeader, sizeof(BITMAPFILEHEADER), &dwWritten, NULL);
	WriteFile(hFile, &bmInfoHeader, sizeof(BITMAPINFOHEADER), &dwWritten, NULL);
	WriteFile(hFile, bmp.bmBits, bmp.bmWidthBytes * bmp.bmHeight, &dwWritten, NULL);
	CloseHandle(hFile);
}
int CcolorDlg::test1()
{
	if (1==2){
	HDC hdcScreen = ::GetDC(hWnd); // 获取屏幕设备上下文句柄
	HDC hdcMemDC = CreateCompatibleDC(hdcScreen); // 创建内存设备上下文句柄，用于保存截屏图像
	int width = GetDeviceCaps(hdcScreen, HORZRES); // 获取屏幕宽度
	int height = GetDeviceCaps(hdcScreen, VERTRES); // 获取屏幕高度
	HBITMAP hBitmap = CreateCompatibleBitmap(hdcScreen, width, height); // 创建位图，用于保存截屏图像
	SelectObject(hdcMemDC, hBitmap); // 将位图与内存设备上下文句柄关联
	BitBlt(hdcMemDC, 0, 0, width, height, hdcScreen, 0, 0, SRCCOPY); // 将屏幕设备上下文句柄中的图像复制到位图中
	CImage image;
	image.Attach(hBitmap);
	image.Save(_T("d:\\text.bmp"));
	
	//SaveBitmapToFile(hBitmap); // 保存位图至文件
	//BYTE* pBitmapData = SaveBitmapToMem(hBitmap); // 保存位图至内存中
	::ReleaseDC(hWnd, hdcScreen); // 释放屏幕设备上下文句柄
	::DeleteDC(hdcMemDC); // 删除内存设备上下文句柄
	::DeleteObject(hBitmap); // 删除位图
	return 0;
	}
	if (1 == 2) {
		HDC hdcScreen = ::GetDC(hWnd); // 
		byte* b1= getHdcAllByte(hdcScreen, 0, 582, 160, 15);



		delete[] b1;
		::ReleaseDC(hWnd, hdcScreen);
		return 0;
	}
	if (1 == 1) {
		HDC hdcScreen = ::GetDC(hWnd); // 94b5ce
		byte* b1 = getHdcByte(hdcScreen, 0, 0,800,600);
		byte bs[10];
		bs[0] = *b1;
		bs[1] = *(b1+1);
		bs[2] = *(b1 + 2);
		bs[3] = *(b1 + 3);
		bs[4] = *(b1 + 4);

		delete[] b1;
		::ReleaseDC(hWnd, hdcScreen);
		return 0;
	}
	if (1 == 2) {
		int a = 0x01020304;
		byte* c = (byte*)&a;
		byte c1 = *c;
		byte c2 = *(c+1);
		byte c3 = *(c + 2);
		byte c4 = *(c + 3);
		return 0;
	}
	return 0;
}

BOOL CcolorDlg::PreTranslateMessage(MSG* pMsg)
{
	if (m_hAccel)
	{
		if (::TranslateAccelerator(m_hWnd, m_hAccel, pMsg))
		{
			return TRUE;
		}
	}
	// TODO: 在此添加专用代码和/或调用基类
	if (pMsg->message == WM_KEYDOWN) // 检查是否是按键按下消息
	{
	}
	else if (pMsg->message == WM_KEYUP) // 检查是否是按键释放消息
	{
		POINT pt;
		//CWnd* pWnd = GetDesktopWindow();
		GetCursorPos(&pt); // 获取当前鼠标位置
		// 例如，检查是否按下了回车键
		UINT m_nCurrentKey; // 当前按下的键
		m_nCurrentKey = pMsg->wParam;
		if (m_nCurrentKey == VK_UP)
		{
			// 设置鼠标位置
			SetCursorPos(pt.x, pt.y-1);
		}
		else if (m_nCurrentKey == VK_DOWN) {
		
			SetCursorPos(pt.x, pt.y+1);
		}
		else if (m_nCurrentKey == VK_LEFT) {

			SetCursorPos(pt.x-1, pt.y);
		}
		else if (m_nCurrentKey == VK_RIGHT) {

			SetCursorPos(pt.x+1, pt.y);
		}

	}

	return CDialogEx::PreTranslateMessage(pMsg);
}


void CcolorDlg::OnEnChangeEdit1()
{
	// TODO:  如果该控件是 RICHEDIT 控件，它将不
	// 发送此通知，除非重写 CDialogEx::OnInitDialog()
	// 函数并调用 CRichEditCtrl().SetEventMask()，
	// 同时将 ENM_CHANGE 标志“或”运算到掩码中。

	// TODO:  在此添加控件通知处理程序代码
	cb1.ResetContent();
	CString str;
	GetDlgItemText(IDC_EDIT1, str);
	CaptureScreen  cs;
	list<WinInfo> cslist1 = cs.GetAllWin(str);
	int i = 0;
	for (auto it = cslist1.begin(); it != cslist1.end(); ++it) {
		WinInfo value = *it; // 访问当前元素
		// 其他代码逻辑...
		//cb1.AddString(value.title);
		cb1.InsertString(i, value.title);
		i++;
	}
	if (!cslist1.empty()) {
		WinInfo value = cslist1.front();
		hWnd = value.hwnd;
		cb1.SetCurSel(0);
		dlglist1 = cslist1;
	}
}


void CcolorDlg::OnCbnSelchangeCombo1()
{
	// TODO: 在此添加控件通知处理程序代码
	 // 获取当前选中项的索引
	int nIndex = cb1.GetCurSel();
	auto iter = dlglist1.begin();

	for (int i = 0; i < nIndex; ++i) {
		++iter;
	}

	//获取第3个元素的值
	WinInfo val = *iter;
	hWnd = val.hwnd;

}
void CcolorDlg::WriteLog(std::string str)
{   
	str = str + "\r\n";
	// 获取当前日志的内容
	CString strText(str.c_str());
	//strText.Format(_T("%s\r\n"), logMessage);
	int length = edit4.GetWindowTextLength();
	edit4.SetSel(length, length, FALSE);
	edit4.SetFocus();

	// 添加新日志并移动光标到文本末尾
	edit4.ReplaceSel(strText);

	//mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
}

void CcolorDlg::OnBnClickedTest1()
{
	// TODO: 在此添加控件通知处理程序代码
	//CString strText;
	/*strText.Format(_T("%d\r\n"), fnt2Dll1(1));
	WriteLog(strText);*/


	/*HMODULE hDll = LoadLibrary(TEXT("C:/soft/source/ocr/ourOut/Release/ppocr.dll"));
	if (hDll != NULL) {
		typedef int(*fnt2Dll1)(int);
		fnt2Dll1 add = (fnt2Dll1)GetProcAddress(hDll, "test1");
		if (add != NULL) {
			int result = add(4);
			strText.Format(_T("%d\r\n"), result);
			WriteLog(strText);
		}
		FreeLibrary(hDll);
	}*/
	if (hWnd == 0) {
		::MessageBox(NULL, _T("请选择窗口"), _T("标题"), MB_OK);
		return;
	}
	if (1 == 2) {
		test1();
		return;
	}
	RECT screenRect;
	//HWND hWnd = ::GetDesktopWindow();
	//hWnd = (HWND)0x000105AA;
	::GetClientRect(hWnd, &screenRect);
	int w = screenRect.right - screenRect.left;
	int h = screenRect.bottom - screenRect.top;
	if (w == 0) {
		::MessageBox(NULL, _T("窗口宽度为0"), _T("标题"), MB_OK);
		return;
	}
	/*if (w>1200) {
			::MessageBox(NULL, _T("宽度大于1200"), _T("标题"), MB_OK);
			return;
		}
	if (h >900) {
		::MessageBox(NULL, _T("高度大于900"), _T("标题"), MB_OK);
		return;
	}*/

	//Screenbmp1(hWnd,208,480,300,570-480, "d:\\text.bmp");
	Screenbmp1(hWnd,0 , 0, 800, 600, "d:\\text1.bmp");
	//Screenbmp1(hWnd, 0, 582,180, 15, "d:\\text.bmp");

}
void CcolorDlg::OnBnClickedTest2() {

	if (hWnd == 0) {
		//::MessageBox(NULL, _T("请选择窗口"), _T("标题"), MB_OK);
		getmax();
		return;
	}
	RECT screenRect;
	::GetClientRect(hWnd, &screenRect);
	int w = screenRect.right - screenRect.left;
	int h = screenRect.bottom - screenRect.top;
	if (w == 0) {
		::MessageBox(NULL, _T("窗口宽度为0"), _T("标题"), MB_OK);
		return;
	}

	CString cstr;
	edit5.GetWindowTextW(cstr);
	// 转换为std::string
	std::string str(CW2A(cstr.GetString()));
	std::vector<std::string> list = split(str,'|');

	int zw = std::stoi(list[2]);
	int zh = std::stoi(list[3]);
	int x = std::stoi(list[0]);
	int y = std::stoi(list[1]);
	int color = hexToDecimal(list[4]);

	Matrix mat(zw,zh);
	char bs[256];
	memset(bs, 0,256);

	HDC hdcScreen = ::GetDC(hWnd); // 
	byte* b1 = getHdcByte(hdcScreen, 0,0, 800,600);
	int c1 = GetBytePixel(b1, 0,0, 800, 600);

	for (int i = 0; i < zw; i++) {
		for (int j = 0; j < zh; j++) {
			int c1 = GetBytePixel(b1, x+ i, y + j, 800,600);
			if (c1 == color) {
				mat.set(i, j, 1);
			}
			else {
				mat.set(i, j, 0);
			}
		}

	}
	CocrMatrix cm;
	cm.toStr(&mat,bs);
	
	string str1=bs;
	WriteLog(str1);

	delete[] b1;
	::ReleaseDC(hWnd, hdcScreen);
}


int CcolorDlg::getmax() {
	HWND hwnd = ::GetDesktopWindow();

	RECT screenRect;
	::GetClientRect(hwnd, &screenRect);
	int w = screenRect.right - screenRect.left;
	int h = screenRect.bottom - screenRect.top;
	if (w == 0) {
		::MessageBox(NULL, _T("窗口宽度为0"), _T("标题"), MB_OK);
		return -1;
	}

	CString cstr;
	edit5.GetWindowTextW(cstr);
	// 转换为std::string
	std::string str(CW2A(cstr.GetString()));
	std::vector<std::string> list = split(str, '|');

	int zw = std::stoi(list[2]);
	int zh = std::stoi(list[3]);
	int x = std::stoi(list[0]);
	int y = std::stoi(list[1]);
	int color = hexToDecimal(list[4]);


	//原点坐标
	CString strx;
	CString stry;
	GetDlgItemText(IDC_EDIT2, strx);
	GetDlgItemText(IDC_EDIT3, stry);
	int numx = std::stoi(strx.GetString());
	int numy = std::stoi(stry.GetString());



	Matrix mat(zw, zh);
	char bs[256];
	memset(bs, 0, 256);

	HDC hdcScreen = ::GetDC(hwnd); // 
	byte* b1 = getHdcByte(hdcScreen, 0, 0,w,h);
	int c1;

	for (int i = 0; i < zw; i++) {
		for (int j = 0; j < zh; j++) {
			int c1 = GetBytePixel(b1, numx+x + i, numy+y + j,w,h);
			if (c1 == color) {
				mat.set(i, j, 1);
			}
			else {
				mat.set(i, j, 0);
			}
		}

	}
	CocrMatrix cm;
	cm.toStr(&mat, bs);

	string str1 = bs;
	WriteLog(str1);

	delete[] b1;
	::ReleaseDC(hwnd, hdcScreen);
	return 0;

}

void CcolorDlg::OnF1()
{
	if (hWnd == 0) {
		::MessageBox(NULL, _T("请选择窗口"), _T("标题"), MB_OK);
		return;
	}
	RECT screenRect;
	//HWND hWnd = ::GetDesktopWindow();
	//hWnd = (HWND)0x000105AA;
	::GetClientRect(hWnd, &screenRect);
	int w = screenRect.right - screenRect.left;
	int h = screenRect.bottom - screenRect.top;
	if (w == 0) {
		::MessageBox(NULL, _T("窗口宽度为0"), _T("标题"), MB_OK);
		return;
	}
	Screenbmp1(hWnd, 0, 0, 800, 600, "d:\\text1.bmp");
}
void CcolorDlg::OnF2()
{  

	// 鼠标坐标
	POINT pt;
	// 获取当前鼠标位置
	GetCursorPos(&pt);

	// 获取鼠标所在位置的窗口句柄
	HWND hWnd = ::WindowFromPoint(pt);

	if (hWnd == 0) {
		::MessageBox(NULL, _T("没有找到窗口"), _T("标题"), MB_OK);
		return;
	}
	RECT screenRect;
	//HWND hWnd = ::GetDesktopWindow();
	//hWnd = (HWND)0x000105AA;
	::GetClientRect(hWnd, &screenRect);
	int w = screenRect.right - screenRect.left;
	int h = screenRect.bottom - screenRect.top;
	if (w == 0) {
		::MessageBox(NULL, _T("窗口宽度为0"), _T("标题"), MB_OK);
		return;
	}
	Screenbmp1(hWnd, 0, 0,w,h, "d:\\text1.bmp");
}


// 响应函数
LRESULT CcolorDlg::OnHotKey(WPARAM wParam, LPARAM lParam) {
	if (wParam == 1001) {
		// 执行自定义操作
		OnF1();
	}
	if (wParam == 1002) {
		// 执行自定义操作
		OnF2();
	}
	return 0;
}